/**
 * jims
 */
package com.jims.master.modules.oper.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.oper.entity.DiseaseManagement;
import com.jims.master.modules.oper.service.DiseaseManagementService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author 系统管理员
 * @version 2017-06-13
 */
@Controller
@RequestMapping(value = "${adminPath}/oper/diseaseManagement")
public class DiseaseManagementController extends BaseController {

	@Autowired
	private DiseaseManagementService diseaseManagementService;
	
	@ModelAttribute
	public DiseaseManagement get(@RequestParam(required=false) String id) {
		DiseaseManagement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = diseaseManagementService.get(id);
		}
		if (entity == null){
			entity = new DiseaseManagement();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DiseaseManagement diseaseManagement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DiseaseManagement> page = diseaseManagementService.findPage(new Page<DiseaseManagement>(request, response), diseaseManagement); 
		model.addAttribute("page", page);
		return "modules/oper/diseaseManagementIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DiseaseManagement diseaseManagement, Model model) {
		model.addAttribute("diseaseManagement", diseaseManagement);
		return "modules/oper/diseaseManagementForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DiseaseManagement diseaseManagement, Model model) {
	    StringData data=new StringData();
		int i=diseaseManagementService.save(diseaseManagement);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DiseaseManagement diseaseManagement) {
	    StringData data=new StringData();
		int i=diseaseManagementService.delete(diseaseManagement);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

	/**
	 * 病种自动补全
	 * @param q
	 * @param limit
	 * @param model
	 * zp
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "automatic")
	@ResponseBody
	public String  automatic(String q,String limit,Model model,String ids) {
		String[] id = null;
		if(!"".equals(ids) && ids != null){
			 id = ids.split(";");
		}

		String retJson= "";
			String orgId= UserUtils.getOrgId();
			List<DiseaseManagement> list = diseaseManagementService.findListDict(id,q, limit);
			String json= "";
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					json+= "{\"id\":\""+list.get(i).getId()+"\",\"text\":\""+list.get(i).getBz()+"\",\"jy\":\""+list.get(i).getJy()+"\"},";
				}
				json = json.substring(0,json.length()-1);
				retJson= "{\"items\": ["+json+"]}";
			}

		return retJson;
	}

}